import request from "@/axios/request";

const {ElMessage} = require("element-plus");

export default {
    name: "HouseDelete",
    data() {
        return {
            search: "",
            currentPage: 1,
            pageSize: 10,
            total: 0,
            allData: [],       // 存储全量数据
            tableData: [],     // 当前页数据
            // 状态映射
            statusMap: {
                rented: { text: '已租', type: 'info' },
                available: { text: '待租', type: 'success' },
                pending: { text: '审核中', type: 'warning' },
                rejected: { text: '审核未通过', type: 'danger' }
            }
        };
    },
    computed: {
        paginatedData() {
            const start = (this.currentPage - 1) * this.pageSize;
            const end = start + this.pageSize;
            return this.allData.slice(start, end);
        }
    },
    created() {
        this.load();
        this.loading = true;
        setTimeout(() => {
            this.loading = false;
        }, 1000);
    },
    methods: {
        async load() {
            try{
                const res = await request.get("/house/findAll");
                if (res.code === 200) {
                    this.allData = res.data || [];
                    this.total = this.allData.length;
                    this.tableData = this.paginatedData;
                } else {
                    ElMessage.error(res.msg || "加载失败");
                    this.allData = [];
                    this.tableData = [];
                    this.total = 0;
                }
                this.loading = false;
            } catch (err){
                console.error(err);
                ElMessage.error("网络错误，请稍后再试");
                this.allData = [];
                this.tableData = [];
                this.total = 0; 
                this.loading = false;
            }
        },
        async filterData() {
            const res = await request.get("/house/findAll");
            if (res.code === 200) {
                this.allData = res.data || [];
                this.total = this.allData.length;
                this.tableData = this.paginatedData;
            } 

            let data = this.allData;
            if (this.search) {
                const id = parseInt(this.search);
                data = data.filter(item => item.houseId === id);
            } else {
                this.load();
            }
            this.allData = data;
            this.total = data.length;
            this.tableData = this.paginatedData;
        },
        reset() {
            this.search = "";
            this.currentPage = 1;
            this.load();
        },
        async handleDelete(row) {
            if(row.status !== 'rented'){
                //删除
                request.delete(`/house/delete/${row.houseId}`).then((res) => {
                    if (res.code === 200) {
                        ElMessage({
                            message: "删除成功",
                            type: "success",
                        });
                        this.load();
                    } else {
                        ElMessage({
                            message: res.msg,
                            type: "error",
                        });
                    }
                });
            }else{
                ElMessage({
                    message: "该房屋已被租用，无法删除",
                    type: "error",
                });
            }
            

        },
        handleSizeChange(pageSize) {
            this.pageSize = pageSize;
            this.tableData = this.paginatedData;
        },
        handleCurrentChange(pageNum) {
            this.currentPage = pageNum;
            this.tableData = this.paginatedData;
        },
                // 状态标签类型获取方法
        getStatusTagType(status) {
            return this.statusMap[status]?.type || '';
        },
        // 状态标签文本获取方法
        getStatusLabel(status) {
            return this.statusMap[status]?.text || '未知状态';
        }
    },
};